<template>
  <div>
    <MesBox
      v-model="model"
      :lazy="true"
      title="选择产品"
      :height="600"
      :width="1200"
      :padding="15"
      :onModelClose="closeModal"
    >
      <!-- 设置查询条件 -->
      <div style="padding-bottom: 10px">
        <span style="margin-right: 10px">产品编码：</span>
        <el-input
          placeholder="请输入产品编码"
          style="width: 200px"
          v-model="productCode"
        />

        <span style="margin-right: 10px">产品名称：</span>
        <el-input
          placeholder="请输入产品名称"
          style="width: 200px"
          v-model="productName"
        />

        <span style="margin-right: 10px" v-if="ShowProductStandard"
          >工程规格：</span
        >
        <el-input
          placeholder="请输入工程规格"
          style="width: 200px"
          v-model="productStandard"
          v-if="ShowProductStandard"
        />

        <!-- <span v-show="inStore" style="margin-left: 20px; margin-right: 10px"
        >入库数量：</span
      >
      <span v-show="!inStore" style="margin-left: 20px; margin-right: 10px"
        >出库数量：</span
      >
      <el-input-number
        :min="1"
        controls-position="right"
        :placeholder="numberPlace"
        style="width: 200px"
        v-model="inStoreQty"
      /> -->

        <el-button
          type="primary"
          style="margin-left: 20px"
          size="default"
          @click="search"
          ><i class="el-icon-search"></i>搜索
        </el-button>
      </div>

      <!-- imes-table配置的这些属性见MesTable组件api文件 -->
      <mes-table
        ref="mytable"
        :loadKey="true"
        :columns="
          $route.path == '/Ware_OutWareHouseBill' ||
          $route.path == '/Ware_SundriesOutMain'
            ? columns1
            : columns
        "
        :pagination="pagination"
        :pagination-hide="false"
        :max-height="600"
        :tableData="tableData"
        :index="true"
        :single="false"
        :defaultLoadPage="defaultLoadPage"
        @loadBefore="loadTableBefore"
        @loadAfter="loadTableAfter"
        @pageNoChange="pageNoChange"
      ></mes-table>
      <!-- 设置弹出框的操作按钮 -->
      <template #footer>
        <div>
          <el-button size="small" type="primary" @click="addRow()"
            ><i class="el-icon-plus"></i>添加选择的数据</el-button
          >
          <el-button size="small" @click="closeModal">
            <i class="el-icon-plus"></i>
            关闭</el-button
          >
        </div>
      </template>
    </MesBox>
  </div>
</template>
<script>
import MesBox from '@/components/basic/MesBox.vue'
import MesTable from '@/components/basic/MesTable.vue'
import { thisTypeAnnotation } from '@babel/types'
export default {
  components: {
    MesBox: MesBox,
    MesTable: MesTable,
  },
  emits: ['parentCall'],
  data() {
    return {
      model: false,
      defaultLoadPage: false, //第一次打开时不加载table数据，openDemo手动调用查询table数据
      productCode: '', //查询条件字段
      productName: '',
      productStandard: '',
      inStoreQty: 1, //入库数量
      inStore: true,
      numberPlace: '请输入入库数量',
      modelType: '',
      columns: [
        {
          field: 'Product_Id',
          title: '产品定义主键ID',
          type: 'int',
          width: 110,
          hidden: true,
          readonly: true,
          require: true,
          align: 'left',
        },
        {
          field: 'ProductCode',
          title: '产品编号',
          type: 'string',
          sort: true,
          width: 250,
          align: 'left',
          sort: true,
        },
        {
          field: 'ProductName',
          title: '产品名称',
          type: 'string',
          link: true,
          sort: true,
          width: 180,
          require: true,
          align: 'left',
        },
        {
          field: 'Unit_Id',
          title: '库存单位',
          type: 'int',
          bind: { key: 'unitList', data: [] },
          width: 110,
          require: true,
          align: 'left',
        },
        {
          field: 'PurchaseStandard',
          title: '采购规格',
          type: 'string',
          width: 180,
          align: 'left',
        },
        {
          field: 'ProductAttribute',
          title: '产品属性',
          type: 'string',
          bind: { key: 'productAttr', data: [] },
          width: 180,
          require: true,
          align: 'left',
        },
        // {
        //   field: 'Process_Id',
        //   title: '工艺路线',
        //   type: 'int',
        //   bind: { key: 'processLineList', data: [] },
        //   width: 110,
        //   align: 'left',
        // },
        // {
        //   field: 'MaxInventory',
        //   title: '最大库存',
        //   type: 'int',
        //   sort: true,
        //   width: 110,
        //   align: 'left',
        // },
        // {
        //   field: 'MinInventory',
        //   title: '最小库存',
        //   type: 'int',
        //   sort: true,
        //   width: 110,
        //   align: 'left',
        // },
        // {
        //   field: 'SafeInventory',
        //   title: '安全库存',
        //   type: 'int',
        //   sort: true,
        //   width: 110,
        //   align: 'left',
        // },
        // {
        //   field: 'InventoryQty',
        //   title: '库存数量',
        //   type: 'int',
        //   sort: true,
        //   width: 110,
        //   align: 'left',
        // },
        // {
        //   field: 'FinishedProduct',
        //   title: '成品图',
        //   type: 'img',
        //   width: 180,
        //   align: 'left',
        // },
      ],
      pagination: {
        total: 0,
        size: 10,
        sizes: [],
        sortName: 'CreateDate',
      }, //分页配置，见mestable组件api
      tableData: [],
      copyData: [],
      page: {
        startIndex: 0,
        endIndex: 0,
      },
      ShowProductStandard: true,
      columns1: [
        {
          title: '产品编码',
          field: 'productCode',
          showOverflowTooltip: true,
        },
        {
          title: '产品名称',
          field: 'productName',
          showOverflowTooltip: true,
        },
        {
          title: '规格',
          field: 'specification',
          showOverflowTooltip: true,
        },
        {
          title: '库存数量',
          field: 'qnty',
          showOverflowTooltip: true,
        },
        {
          title: '类型',
          field: 'type',
          showOverflowTooltip: true,
        },
        {
          title: '料号',
          field: 'materialNum',
          showOverflowTooltip: true,
        },
        {
          title: '批次',
          field: 'lotNo',
          showOverflowTooltip: true,
        },
        {
          title: '客户',
          field: 'client',
          showOverflowTooltip: true,
        },
        {
          title: '添加时间',
          field: 'addTime',
          showOverflowTooltip: true,
        },
        // {
        //   title: '创建时间',
        //   field: 'createDate',
        //   showOverflowTooltip: true,
        // },
        {
          title: '创建人',
          field: 'creator',
          showOverflowTooltip: true,
        },
        // {
        //   title: '修改时间',
        //   field: 'modifyDate',
        //   showOverflowTooltip: true,
        // },
        // {
        //   title: '修改人',
        //   field: 'modifier',
        //   showOverflowTooltip: true,
        // },
      ],
      dicName: '',
    }
  },
  mounted() {
    this.getMaterial()
  },
  methods: {
    async getMaterial() {
      // console.log(this.$route.path)
      if (
        this.$route.path == '/Ware_WareHouseBill' ||
        this.$route.path == '/Ware_SundriesInsetMain'
      ) {
        let res = await this.http.get(
          'api/Base_BomDetailTest/GetProductMatrail',
          null,
          true
        )

        this.tableData = res
        this.copyData = this.tableData.map((item) => {
          return {
            ProductName: item.ProductName ? item.ProductName : '',
            ProductCode: item.ProductCode ? item.ProductCode : '',
            ProductStandard: item.ProductStandard ? item.ProductStandard : '',
            PurchaseStandard: item.PurchaseStandard ? item.PurchaseStandard : '',
            Unit_Id: item.Unit_Id ? item.Unit_Id : '',
            ProductAttribute: item.ProductAttribute
              ? item.ProductAttribute
              : '',
            DicName: item.DicName ? item.DicName : '',
          }
        })
        this.ccData = this.tableData
        this.pagination.total = this.tableData.length
      }
    },
    open(outType) {
      this.model = true
      this.inStore = this.$route.path == '/Ware_OutWareHouseBill' ? false : true
      this.numberPlace =
        this.$route.path == '/Ware_OutWareHouseBill'
          ? '请输入出库数量'
          : '请输入入库数量'
      //打开弹出框时，加载table数据
      this.$nextTick(() => {
        // this.$refs.mytable.load()
        this.search(outType)
      })
    },
    async search(outType) {
      if (this.$route.path == '/Ware_OutWareHouseBill') {
        let type = this.productName || this.productCode ? 'type2' : 'type1'
        let res = await this.http.post(
          `/api/Ware_OutWareHouseBill/selectWarehouse?type=${type}&productcode=${this.productCode}&productname=${this.productName}`,
          null,
          true
        )
        if (outType == 'outsource')
          this.tableData = res.data.filter((item) => item.type == '原材料')
        else this.tableData = res.data
        // console.log(this.pagination)
        this.pagination.size = this.tableData.length
        this.$refs.mytable.handleSizeChange(this.tableData.length)
        this.$refs.mytable.changeTotal(this.tableData.length)
      } else if (this.$route.path == '/Ware_SundriesOutMain') {
        const res = await this.http.post(
          '/api/Ware_SundriesOutMain/GETsunWarehouse',
          null,
          true
        )
        this.tableData = res.data
        this.pagination.total = this.tableData.length
        this.$refs.mytable.handleSizeChange(this.tableData.length)
        this.$refs.mytable.changeTotal(this.tableData.length)
      } else {
        // console.log(outType);
        if (this.dicName == '') {
          switch (outType) {
            case 'customerreturn':
              this.dicName = '产成品'
              break
            case 'outsource':
              this.dicName = '原材料'
              break
            case 'other':
              this.dicName = '原材料包辅材半成品产成品'
              break
            case 'purchase':
              this.dicName = '原材料包辅材'
              break
            case 'SectionMateriel':
              this.dicName = '原材料包辅材'
              break
            case 'SpecialMateriel':
              this.dicName = '原材料包辅材'
              break
            default:
              break
          }
        }

        if (this.$route.path == '/Ware_SundriesInsetMain') this.dicName = '劳保'

        this.pageNoChange(1)
      }
    },
    getFieldDicValue(fieldName, fieldValue) {
      this.columns.forEach((item) => {
        if (item.field == fieldName) {
          var result = item.bind.data.find((val) => val.key == fieldValue)
          return result
        }
      })
    },
    addRow() {
      var rows = this.$refs.mytable.getSelected()
      if (!rows || rows.length == 0) {
        return this.$message.error('请选择行数据')
      }
      if (this.inStoreQty == '') {
        return this.$message.error(this.numberPlace)
      }
      //获取回写到明细表的字段
      let _rows = rows.map((row) => {
        console.log(row)
        if (
          this.$route.path == '/Ware_OutWareHouseBill' ||
          this.$route.path == '/Ware_SundriesOutMain'
        ) {
          return {
            Product_Id: '',
            ProductCode: row.productCode,
            ProductName: row.productName,
            Unit_Id: '',
            ProductStandard: row.specification,
            ProductAttribute: '',
            Process_Id: '',
            MaxInventory: '',
            MinInventory: '',
            SafeInventory: '',
            InventoryQty: '',
            OutStoreQty: 1,
            Type: row.type,
          }
        } else {
          return {
            Product_Id: row.Product_Id,
            ProductCode: row.ProductCode,
            ProductName: row.ProductName,
            Unit_Id: row.Unit_Id,
            ProductStandard: row.PurchaseStandard,
            ProductAttribute: row.ProductAttribute,
            Process_Id: row.Process_Id,
            MaxInventory: row.MaxInventory,
            MinInventory: row.MinInventory,
            SafeInventory: row.SafeInventory,
            InventoryQty: row.InventoryQty,
            InStoreQty: this.inStoreQty,
            WareHouseBillType: row.DicName,
          }
        }
      })
      this.$emit('parentCall', ($parent) => {
        $parent.$refs.detail.rowData.unshift(..._rows)
        $parent.$refs.detail.changeTotal($parent.$refs.detail.rowData.length)
      })
      //关闭当前窗口
      this.model = false
    },
    //这里是从api查询后返回数据的方法
    loadTableAfter(row) {},
    loadTableBefore(params) {
      //查询前，设置查询条件
      if (this.productCode) {
        params.wheres.push({
          name: 'ProductCode',
          value: this.productCode,
          displayType: 'like',
        })
      }
      if (this.productName) {
        params.wheres.push({
          name: 'ProductName',
          value: this.productName,
          displayType: 'like',
        })
      }
      return true
    },
    pageNoChange(currentPage) {
      let startIndex = (currentPage - 1) * this.pagination.size
      let endIndex = startIndex + this.pagination.size
      this.ccData = this.copyData.filter((item) => {
        return (
          item.ProductCode.indexOf(this.productCode) !== -1 &&
          item.ProductName.indexOf(this.productName) !== -1 &&
          item.ProductStandard.indexOf(this.productStandard) !== -1 &&
          this.dicName.indexOf(item.DicName) !== -1
        )
      })
      this.tableData = this.ccData.slice(startIndex, endIndex)
      this.$refs.mytable.changeTotal(this.ccData.length)
    },
    closeModal() {
      this.model = false
      this.dicName = ''
    },
  },
}
</script>
